package view;

import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import controller.Controller;
import view.actionListeners.AddPieceListener;

/**
 * Class represents a panel to add the pieces (all but King) for each player on partial game mode
 */
public class AddPiecePanel extends JPanel {

	// members
	
	// set serializing id
	private static final long serialVersionUID = 1L;
	
	// button strings
	private static final String PAWN_STRING = "Pawn";
	private static final String KNIGHT_STRING = "Knight";
	private static final String BISHOP_STRING = "Bishop";
	private static final String ROOK_STRING = "Rook";
	private static final String QUEEN_STRING = "Queen";
	private static final String CONFIRM_STRING = "Confirm";
	private static final String DONE_STRING = "Done";
	private static final String DEFAULT_PLAYER = "White";
	
	// piece buttons
	private JRadioButton pawnBtn;
	private JRadioButton knightBtn;
	private JRadioButton bishopBtn;
	private JRadioButton rookBtn;
	private JRadioButton queenBtn;
	private ButtonGroup pieceGroup;

	// combo-box to choose position
	private JComboBox<Integer> rowIndexBox;
	private JComboBox<Integer> colIndexBox;
	private Integer[] indices = {1, 2, 3, 4, 5, 6, 7, 8};
	
	// action buttons
	private JButton confirmBtn;
	private JButton doneBtn;
	private ButtonGroup actionGroup;
	
	// current player label
	private JLabel currentPlayer;
	
	// inner panels
	private JPanel pieceHolder;
	private JPanel positionChooser;
	private JPanel actionPanel;
	private JPanel mainPanel;
	
	// public methods
	
	/**
	 * Constructor- creates a new panel for adding pieces to board
	 * @param frameManager- manages game display's frame
	 * @param gameManager- manages game
	 * @param maxPieces- maximal pieces for each player in partial game 
	 */
	public AddPiecePanel(ChessGUI frameManager, Controller gameManager, int maxPieces) {
		
		// allocate piece buttons
		pawnBtn = new JRadioButton(PAWN_STRING);
		knightBtn = new JRadioButton(KNIGHT_STRING);
		bishopBtn = new JRadioButton(BISHOP_STRING);
		rookBtn = new JRadioButton(ROOK_STRING);
		queenBtn = new JRadioButton(QUEEN_STRING);
		
		// set action command for each piece button
		pawnBtn.setActionCommand(PAWN_STRING);
		knightBtn.setActionCommand(KNIGHT_STRING);
		bishopBtn.setActionCommand(BISHOP_STRING);
		rookBtn.setActionCommand(ROOK_STRING);
		queenBtn.setActionCommand(QUEEN_STRING);
		
		// group the piece buttons
		pieceGroup = new ButtonGroup();
		pieceGroup.add(pawnBtn);
		pieceGroup.add(knightBtn);
		pieceGroup.add(bishopBtn);
		pieceGroup.add(rookBtn);
		pieceGroup.add(queenBtn);
		
		// place piece buttons on panel
		pieceHolder = new JPanel(new GridLayout(5,1));
		pieceHolder.add(pawnBtn);
		pieceHolder.add(knightBtn);
		pieceHolder.add(bishopBtn);
		pieceHolder.add(rookBtn);
		pieceHolder.add(queenBtn);
		
		// set combo-boxes for position
		rowIndexBox = new JComboBox<Integer>(indices);
		colIndexBox = new JComboBox<Integer>(indices);
		
		// place combo-boxes on panel
		JLabel rowLable = new JLabel("Row");
		JLabel colLable = new JLabel("Column");
		positionChooser = new JPanel(new GridLayout(4,1));
		positionChooser.add(rowLable);
		positionChooser.add(rowIndexBox);
		positionChooser.add(colLable);
		positionChooser.add(colIndexBox);
		
		// set current player label
		currentPlayer = new JLabel(DEFAULT_PLAYER);
		
		// allocate action buttons
		confirmBtn = new JButton(CONFIRM_STRING);
		doneBtn = new JButton(DONE_STRING);
		
		// set action command for each action button
		confirmBtn.setActionCommand(CONFIRM_STRING);
		doneBtn.setActionCommand(DONE_STRING);
		
		// set action buttons listeners
		AddPieceListener addPieceListener = new AddPieceListener(this, gameManager, frameManager, maxPieces, CONFIRM_STRING, DONE_STRING, currentPlayer);
		confirmBtn.addActionListener(addPieceListener);
		doneBtn.addActionListener(addPieceListener);
		
		// group the action buttons
		actionGroup = new ButtonGroup();
		actionGroup.add(confirmBtn);
		actionGroup.add(doneBtn);
		
		// set action buttons on panel
		actionPanel = new JPanel(new GridLayout(2,1)); // TODO add spaces
		actionPanel.add(confirmBtn);
		actionPanel.add(doneBtn);
		
		// set panel borders
		pieceHolder.setBorder(BorderFactory.createTitledBorder("Piece"));				
		positionChooser.setBorder(BorderFactory.createTitledBorder("Position"));
		currentPlayer.setBorder(BorderFactory.createTitledBorder("Color"));
		
		// add components to main panel
		mainPanel = new JPanel(new GridLayout(4,1,10,10));
		mainPanel.add(pieceHolder);		
		mainPanel.add(positionChooser);
		mainPanel.add(currentPlayer);
		mainPanel.add(actionPanel);
		add(mainPanel);
	}
	
	/**
	 * Returns selected row index
	 * @return value of selected row index
	 */
	public int getRowIndex() { return rowIndexBox.getSelectedIndex() + 1; }
	
	/**
	 * Returns selected column index
	 * @return value of selected column index
	 */
	public int getColIndex() { return colIndexBox.getSelectedIndex() + 1; }

	/**
	 * Returns type of piece that user has selected to add.
	 * If no button is selected, returns null
	 * @return type of piece to be added
	 */
	public String getSelectedPiece() {
		
		// check if user has selected Pawn
		if (pawnBtn.isSelected()) {	return "Pawn";	}
		
		// check if user has selected Knight
		if (knightBtn.isSelected()) { return "Knight"; }
		
		// check if user has selected Bishop
		if (bishopBtn.isSelected()) { return "Bishop"; }
		
		// check if user has selected Rook
		if (rookBtn.isSelected()) { return "Rook"; }
				
		// check if user has selected Queen
		if (queenBtn.isSelected()) { return "Queen"; }
		
		// no button is selected
		return null;
	}
}